package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.Constant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.dto.TagDTO;
import com.xfcy.blog.mapper.TagsMapper;
import com.xfcy.blog.entity.Tags;
import com.xfcy.blog.service.TagsService;
import com.xfcy.blog.utils.BeanCopyUtil;
import com.xfcy.blog.vo.TagVO;
import com.xfcy.blog.vo.TagsCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 博客标签表(Tags)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:06
 */
@Service("tagsService")
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public R listByRecord(Integer pageNo, Integer pageSize, String name) {
        Page<TagsCountVO> tagsPage = baseMapper.selectPageRecord(new Page<>(pageNo, pageSize), name);
        return R.success(tagsPage);
    }

    /**
     * 取消置顶或置顶
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R top(Long id) {
        Tags tags = baseMapper.selectOne(new QueryWrapper<Tags>().select(SqlConstant.SORT).eq(SqlConstant.ID, id));
        if (Objects.equals(tags.getSort(), Constant.ZERO)) {
            tagsMapper.updateSort(id, Constant.ONE);
        } else {
            tagsMapper.updateSort(id, Constant.ZERO);
        }
        return R.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteById(Long id) {
        baseMapper.deleteById(id);
        baseMapper.deleteTagId(Collections.singletonList(id));
        return R.success();
    }

    /**
     * 批量删除标签id
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatch(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
        baseMapper.deleteTagId(ids);
        return R.success();
    }

    /**
     * 添加标签
     * @param tagDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addTag(TagDTO tagDTO) {
        Tags tags = BeanCopyUtil.copyObject(tagDTO, Tags.class);
        int insert = baseMapper.insert(tags);
        return insert > 0 ? R.success() : R.error();
    }

    /**
     * 标签详情
     * @param id
     * @return
     */
    @Override
    public R getTagsById(Long id) {
        Tags tags = baseMapper.selectById(id);
        TagVO tagVO = BeanCopyUtil.copyObject(tags, TagVO.class);
        return R.success(tagVO);
    }

    /**
     * 更改标签
     * @param tagDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateTag(TagDTO tagDTO) {
        Tags tags = BeanCopyUtil.copyObject(tagDTO, Tags.class);
        int i = baseMapper.updateById(tags);
        return i > 0 ? R.success() : R.error();
    }


    // 前台Web方法

    /**
     *  前台标签列表
     * @return
     */
    @Override
    public R tagList() {
        List<TagVO> list = tagsMapper.selectAll();
        return R.success(list);
    }
}


